﻿namespace NRapid
{
    public class ActionSearch
    {
        public static int Search(string requestedPath)
        {
			//重写虚拟目录
			${set(rewriteDirectoryList=rewriteData.rewriteDirectoryList)}
			${foreach(rewriteDirectory in rewriteDirectoryList)}
			if(requestedPath.StartsWith("${rewriteDirectory.from}"))
			{
				requestedPath = "${rewriteDirectory.to}" + requestedPath.Remove(0,"${rewriteDirectory.from}".Length);
			}
			${end}
			//重写虚拟路径,也可以把此部分放到find中
			switch (requestedPath)
			{
				//网站图标
				case "/favicon.ico": return 0;
				${set(rewriteFileList=rewriteData.rewriteFileList)}
				${foreach(rewriteFile in rewriteFileList)}
				case "${rewriteFile.from}":requestedPath = "${rewriteFile.to}"; break;
				${end}
				default: break;
			}
            //获取actionUrl
            string actionUrl = GetActionUrl(requestedPath);
            int find = GetActionId(actionUrl, requestedPath);
            return find;
        }
        /// <summary>
        /// 计算actionUrl
        /// </summary>
        /// <param name="requestedPath"></param>
        /// <returns></returns>
        private static string GetActionUrl(string requestedPath)
        {
            //找到最后一个.的位置
            int position = requestedPath.LastIndexOf('.');
            bool hasExtension = false;
            
            //判断是否有后缀（后缀首字母不能为数字，且长度必须大于0小于5）
            int extesionLength = requestedPath.Length - position-1;
            if (extesionLength > 0 && extesionLength < 5)
            {
                char ch = requestedPath[position + 1];
				//.后面不是数字
                if (ch > '9' || ch < '0')
                {
					hasExtension = true;
                }
            }
            
            //指向ActionUrl结尾的位置
            int pointer = 0;
            string actionUrl = requestedPath;
            bool hasGet = false;
            //如果有后缀
            if (hasExtension)
            {
                hasGet = int.TryParse(requestedPath.Substring(position - 2, 2), out pointer);
            }
            //如果没有后缀
            else
            {
                hasGet = int.TryParse(requestedPath.Substring(requestedPath.Length - 2, 2), out pointer);
            }
            //取出ActionUrl
            if (hasGet)
            {
                if (pointer > -1 && pointer < requestedPath.Length)
                {
                    actionUrl = requestedPath.Substring(0, pointer + 1);
                }
            }
            return actionUrl;
        }
        /// <summary>
        /// 查询actionUrl
        /// </summary>
        /// <param name="app"></param>
        /// <param name="actionUrl"></param>
        /// <param name="requestedPath"></param>
        /// <returns></returns>
        private static int GetActionId(string actionUrl, string requestedPath)
        {
            int find = 0;
            //无参数
            switch (requestedPath)
            {
                ${set(i=1)}
				${foreach(fileData in controllerFileDataList)}
				${set(fileDataClassDataList=fileData.ClassDataList)}
				${foreach(classData in fileDataClassDataList)}
				${set(classDataMethodDataList=classData.MethodDataList)}
				${foreach(methodData in classDataMethodDataList)}
                ${if(methodData.isPublic)}
					${if(methodData.hasParameters==false)}
				case "${methodData.urlParser.actionUrl}":
                    find = ${i}; break;
					${end}
				${set(i=i+1)}
				${end}
				${end}
				${end}
				${end}
                default: find = 0; break;
            }
			//有参数
			if(find==0)
			{
				switch (actionUrl)
				{
					${set(i=1)}
					${foreach(fileData in controllerFileDataList)}
					${set(fileDataClassDataList=fileData.ClassDataList)}
					${foreach(classData in fileDataClassDataList)}
					${set(classDataMethodDataList=classData.MethodDataList)}
					${foreach(methodData in classDataMethodDataList)}
					${if(methodData.isPublic)}
						${if(methodData.hasParameters==true)}
					case "${methodData.urlParser.actionUrl}":
						find = ${i}; break;
						${end}
					${set(i=i+1)}
					${end}
					${end}
					${end}
					${end}
					default: find = 0; break;
				}
			}
            return find;
        }
    }
}